using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Animator;

namespace TurbaconPhone
{
    public class EndGameState : State
    {
        SpriteFont Font;
        Texture2D shade;
        float fade = 0f;
        float Fadein = 1.0f;
        Character guyF, guyL;

        public EndGameState()
        {
            Font = TurBacon.Content.GetFont("bigFont");
            shade = TurBacon.Content.GetTexture("1x1");
            

        }

        public override void EnterState()
        {
            if (TurBacon.GameInfo.WinningTeam == CharacterTeam.Red)
            {
                CharDef def = new CharDef("Data/red.zmx");
                guyF = new Character(new Vector2(TurBacon.Game.GraphicsDevice.Viewport.Width / 2 + 200, TurBacon.Game.GraphicsDevice.Viewport.Height + 100), def, PlayerType.forward, CharacterTeam.Red, Character.CharDir.Right);
                guyF.SetAnim(Character.ANIM_VICTORY);
                guyF.Scale = 3.0f;
                def = new CharDef("Data/blue.zmx");
                guyL = new Character(new Vector2(TurBacon.Game.GraphicsDevice.Viewport.Width / 2-200, TurBacon.Game.GraphicsDevice.Viewport.Height - 100), def, PlayerType.forward, CharacterTeam.Blue, Character.CharDir.Right);
                guyL.SetAnim(Character.ANIM_DEAD);
                guyL.Face = Character.CharDir.Right;
                guyL.Scale = 2.0f;
            }
            else
            {
                CharDef def = new CharDef("Data/blue.zmx");
                guyF = new Character(new Vector2(TurBacon.Game.GraphicsDevice.Viewport.Width / 2 + 200, TurBacon.Game.GraphicsDevice.Viewport.Height + 100), def, PlayerType.forward, CharacterTeam.Blue, Character.CharDir.Right);
                guyF.SetAnim(Character.ANIM_VICTORY);
                guyF.Scale = 3.0f;
                def = new CharDef("Data/red.zmx");
                guyL = new Character(new Vector2(TurBacon.Game.GraphicsDevice.Viewport.Width / 2 - 200, TurBacon.Game.GraphicsDevice.Viewport.Height - 100), def, PlayerType.forward, CharacterTeam.Blue, Character.CharDir.Right);
                guyL.SetAnim(Character.ANIM_DEAD);
                guyL.Face = Character.CharDir.Right;
                guyL.Scale = 2.0f;
            }
            EnabledGestures = GestureType.Tap;
            base.EnterState();
        }
        public override void ExitState()
        {
            base.ExitState();
            guyF.Die();
            guyL.Die();
        }
        public override void Update(GameTime gametime)
        {
            base.Update(gametime);
            if(fade < Fadein)
                fade += (float)gametime.ElapsedGameTime.TotalSeconds;
            guyF.Update(gametime);
            guyL.Update(gametime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(shade, new Rectangle(0, 0, TurBacon.Game.GraphicsDevice.Viewport.Width, TurBacon.Game.GraphicsDevice.Viewport.Height), new Color(new Vector4(0,0,0, 1- (Fadein - fade) - 0.3f)));
            Color fadeColor = ((TurBacon.GameInfo.WinningTeam == CharacterTeam.Red) ? new Color(new Vector4(1,0,0,1- (Fadein - fade) - 0.3f)) : new Color(new Vector4(0,0,1,1- (Fadein - fade) - 0.3f)));

            spriteBatch.DrawString(Font, "A WINNER IS " + ((TurBacon.GameInfo.WinningTeam == CharacterTeam.Red) ? "RED!" : "BLUE!"),  new Vector2(TurBacon.Game.GraphicsDevice.Viewport.Width/2, TurBacon.Game.GraphicsDevice.Viewport.Height/2) - Font.MeasureString("A WINNER IS BLUE!")/2, fadeColor);
            spriteBatch.End();
        }

        public override void HandleTap(GestureSample gesture)
        {
            TurBacon.StateManager.Pop();
            base.HandleTap(gesture);
        }
    }

 
}
